import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    commodities: [
      { name: '手机', price: 1000, quantity: 1 },
      { name: '电脑', price: 2000, quantity: 1 },
      { name: '键盘', price: 200, quantity: 2 }
    ]
  },
  getters: {
    total(state) {
      return state.commodities.reduce((sum, item) => sum + item.price * item.quantity, 0);
    }
  },
  mutations: {
    increaseQuantity(state, index) {
      state.commodities[index].quantity++;
    },
    decreaseQuantity(state, index) {
      if (state.commodities[index].quantity > 0) {
        state.commodities[index].quantity--;
      }
    }
  },
  actions: {
  },
  modules: {
  }
})